package org.jeecg.modules.jxc.inventorymanagement.commodityloss.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.jxc.homepage.entity.JxcGoods;
import org.jeecg.modules.jxc.homepage.service.IJxcGoodsService;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.entity.JxcDamageLisjxcGoods;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.entity.JxcDamageList;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.mapper.JxcDamageLisjxcGoodsMapper;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.mapper.JxcDamageListMapper;
import org.jeecg.modules.jxc.inventorymanagement.commodityloss.service.IJxcDamageLisjxcGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: jxc_damage_lisjxc_goods
 * @Author: jeecg-boot
 * @Date: 2021-12-21
 * @Version: V1.0
 */
@SuppressWarnings("all")
@Service
public class JxcDamageLisjxcGoodsServiceImpl extends ServiceImpl<JxcDamageLisjxcGoodsMapper, JxcDamageLisjxcGoods> implements IJxcDamageLisjxcGoodsService {
   @Resource
   private JxcDamageLisjxcGoodsMapper jxcDamageLisjxcGoodsMapper;
   @Resource
   private JxcDamageListMapper jxcDamageListMapper;
   @Resource
   private IJxcGoodsService jxcGoodsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveDamage(String damageNumber, String damageListGoodsStr, JxcDamageLisjxcGoods[] jxcDamageLisjxcGoodsList,Integer userId) {
        //1.报损订单数据新增(订单编号的)
        //获取用户id
        JxcDamageList jxcDamageList = new JxcDamageList();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        try {
            jxcDamageList.setDamageDate(format);
            jxcDamageList.setCreateTime(date);
            jxcDamageList.setDamageNumber(damageNumber);
            jxcDamageList.setRemarks(damageListGoodsStr);
            jxcDamageList.setDelFlag(0);
            jxcDamageList.setUserId(userId);
            jxcDamageList.setCreateBy("zahngsan");
            System.out.println(jxcDamageList);
            System.out.println(jxcDamageList.getId()+"============");

            jxcDamageListMapper.insert(jxcDamageList);

            System.out.println(jxcDamageList.getId()+"============");
            Integer id = jxcDamageList.getId();//报损单ID
            //2.报损获取新增
            System.out.println(jxcDamageLisjxcGoodsList+"=======++++++++++=====");

            for (int i = 0; i < jxcDamageLisjxcGoodsList.length; i++) {

                JxcDamageLisjxcGoods jxcDamageLisjxcGoods = jxcDamageLisjxcGoodsList[i];
                jxcDamageLisjxcGoods.setDamageLisjxcId(id);//设置报损单ID
                jxcDamageLisjxcGoods.setCreateTime(date);
                jxcDamageLisjxcGoods.setCreateBy("zahngsan");
                jxcDamageLisjxcGoods.setDelFlag(0);

                jxcDamageLisjxcGoodsMapper.insert(jxcDamageLisjxcGoods);
                //3.减库存
                Integer goodsId = jxcDamageLisjxcGoods.getGoodsId();

                JxcGoods jxcGoods = new JxcGoods();
                jxcGoods.setUpdateBy("zahngsan");
                jxcGoods.setUpdateTime(date);
                JxcGoods jxcGoods1 = jxcGoodsService.getOne(new QueryWrapper<JxcGoods>().eq("id", goodsId));
                jxcGoods1.setInventoryQuantity(jxcGoods1.getInventoryQuantity() - jxcDamageLisjxcGoods.getGoodsNum());
                jxcGoodsService.updateById(jxcGoods1);
            }
        }catch (Exception e){
            log.error("错误");
            e.printStackTrace();
            throw e;
        }

    }

    @Override
    public void saveDamage(String damageNumber, String damageListGoodsStr, List<JxcDamageLisjxcGoods>[] jxcDamageLisjxcGoodsList) {

    }

    @Override
    public List<Map<String, Object>> queryList(String sTime) {
        List<Map<String, Object>> list = jxcDamageListMapper.selectListByTime(sTime);
        return list;
    }

    @Override
    public List<Map<String, Object>> queryGoodsList(Integer damageListId) {

        return jxcDamageListMapper.selectListById(damageListId);
    }
}
